import PureCompanyRunnableGroups from '../components/runnable-groups';
import * as CompanyAction from 'actions/company';
import * as RunnableGroupAction from 'actions/runnable-group';
import { getFetchStatus } from 'actions/util';
import * as React from 'react';
import * as ReactRedux from 'react-redux';

const mapStateToProps = (state) => {
  return {
    fetching: getFetchStatus(state, CompanyAction.listRunnableGroups)
    || getFetchStatus(state, CompanyAction.listMachines)
    || getFetchStatus(state, CompanyAction.listAuthedDevTeams),
    runnableGroups: state.companyRunnableGroupList,
    companyMachines: state.companyMachineList,
    companyTeams: state.companyAuthedDevTeamList
  };
};
const mapDispatchToProps = (dispatch, ownProps) => {
  const { companyId } = ownProps;

  return {
    initFetch: () => {
      return Promise.all([
          dispatch(CompanyAction.listMachines(companyId)),
          dispatch(CompanyAction.listAuthedDevTeams(companyId))
        ])
        .then(() => {
          dispatch(CompanyAction.listRunnableGroups(companyId));
        });
    },
    onAssign: (groupId, teamId) => {
      return dispatch(RunnableGroupAction.assign(groupId, teamId));
    },
    onUpdateMachines: (groupId, machineIds) => {
      return dispatch(RunnableGroupAction.updateMachines(groupId, machineIds));
    }
  };
};

class CompanyRunnableGroups extends React.Component {
  componentDidMount() {
    this.props.initFetch();
  }

  render() {
    return <PureCompanyRunnableGroups {...this.props}/>;
  }
}

export default ReactRedux.connect(mapStateToProps, mapDispatchToProps)(CompanyRunnableGroups);
